package com.kk.app;

import android.content.Context;
import android.hardware.display.DisplayManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.kk.app.databinding.ActivityMainBinding;
import com.kk.app.model.PlayerModel;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements IMediaControl {

    ActivityMainBinding mBinding = null;
    MediaPlayer mPlayer = null;

    private static final String TAG = "[player]";

    private PlayerModel mPlayerModel = new PlayerModel();

    private PlayerPresentation demoChatPresentation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RenderData.init();
        mPlayer = RenderData.getMediaPlayer();
        mBinding = ActivityMainBinding.inflate(LayoutInflater.from(this));
        mPlayerModel.setMediaControl(this);
        mBinding.setData(mPlayerModel);
        setContentView(mBinding.getRoot());

        getPermission();

    }

    private void getPermission() {

        PermissionManager.getInstance().checkPermission(this, new PermissionManager.Callback() {
            @Override
            public void result(boolean isSuccess) {
                PermissionManager.getInstance().release();
                if (isSuccess) {
                } else {
                    Toast.makeText(MainActivity.this, "请授予需要的权限，不然程序无法正常使用", Toast.LENGTH_LONG).show();
                    getPermission();
                }
            }
        });
    }

    private void initListener() {

        mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.start();
            }
        });

        mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {

            }
        });

        mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
                return false;
            }
        });
    }


    private String getVideoPath() {
        return "/sdcard/844065.ts";
    }


    @Override
    public void nextSong() {
        Log.i(TAG, "nextSong: 1111");
        try {
            mPlayer.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            mPlayer.reset();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            initListener();
            mPlayer.setDataSource(getVideoPath());
            mPlayer.prepareAsync();
        } catch (IOException e) {
            Log.i(TAG, "nextControl: ");
        }
    }

    @Override
    public void showMVDisplay(boolean show) {

        /**
         * 只显示一次
         */
        if (show) {
            if (demoChatPresentation == null) {
                DisplayManager displayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE);
                Display[] displays = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
                if (displays.length > 0) {
                    demoChatPresentation = new PlayerPresentation(MainActivity.this, displays[0], mPlayer);
                }
                if (demoChatPresentation != null && !demoChatPresentation.isShowing()) {
                    demoChatPresentation.show();
                }
            }
        }

    }


    private volatile boolean mTestIsRunning = false;

    private long mLastStartTestTime = 0;

    @Override
    public void autoTestControl(boolean start) {
        if (start) {

            /**
             * 连续两次开始不能小于5S
             */
            if (!mTestIsRunning && ((System.currentTimeMillis() - mLastStartTestTime) > 5000)) {
                mTestIsRunning = true;
                mLastStartTestTime = System.currentTimeMillis();
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        while (true) {
                            try {
                                Thread.sleep(3000);
                                nextSong();
                            } catch (InterruptedException e) {
                                Log.e(TAG, "run: " + e.getMessage());
                            }
                        }
                    }
                }).start();
            }

        } else {
            mTestIsRunning = false;
        }
    }
}